import 'package:flutter/material.dart';
import 'dart:math' as math;

class HandDrawnEmoji extends StatelessWidget {
  final double intensity;
  final double size;
  final Color color;

  const HandDrawnEmoji({
    Key? key,
    required this.intensity,
    this.size = 64,
    this.color = Colors.black,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(size, size),
      painter: _HandDrawnEmojiPainter(
        intensity: intensity,
        color: color,
      ),
    );
  }
}

class _HandDrawnEmojiPainter extends CustomPainter {
  final double intensity;
  final Color color;

  _HandDrawnEmojiPainter({
    required this.intensity,
    required this.color,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0
      ..strokeCap = StrokeCap.round;

    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2 - 8;

    // 画手绘风格的圆形脸
    final facePath = Path();
    final random = math.Random(42); // 固定随机种子以保持一致性
    
    double startAngle = 0;
    const segments = 8;
    const variance = 0.08; // 控制不规则程度
    
    for (int i = 0; i <= segments; i++) {
      final angle = 2 * math.pi * i / segments;
      final radiusVariation = radius * (1 + random.nextDouble() * variance - variance / 2);
      final x = center.dx + math.cos(angle) * radiusVariation;
      final y = center.dy + math.sin(angle) * radiusVariation;
      
      if (i == 0) {
        facePath.moveTo(x, y);
      } else {
        final controlPoint1x = center.dx + math.cos(startAngle + (angle - startAngle) / 3) * radius * 1.1;
        final controlPoint1y = center.dy + math.sin(startAngle + (angle - startAngle) / 3) * radius * 1.1;
        final controlPoint2x = center.dx + math.cos(angle - (angle - startAngle) / 3) * radius * 1.1;
        final controlPoint2y = center.dy + math.sin(angle - (angle - startAngle) / 3) * radius * 1.1;
        
        facePath.cubicTo(
          controlPoint1x, controlPoint1y,
          controlPoint2x, controlPoint2y,
          x, y
        );
      }
      startAngle = angle;
    }
    
    canvas.drawPath(facePath, paint);

    // 画眼睛
    final eyeBaseSize = size.width / 12;
    final leftEyeCenter = Offset(
      center.dx - radius * 0.35,
      center.dy - radius * 0.1
    );
    final rightEyeCenter = Offset(
      center.dx + radius * 0.35,
      center.dy - radius * 0.1
    );

    // 根据情绪强度画不同的眼睛
    if (intensity >= 8) {
      // 开心 - 弯弯的眼睛
      final leftEyePath = Path();
      leftEyePath.moveTo(
        leftEyeCenter.dx - eyeBaseSize,
        leftEyeCenter.dy
      );
      leftEyePath.quadraticBezierTo(
        leftEyeCenter.dx,
        leftEyeCenter.dy - eyeBaseSize,
        leftEyeCenter.dx + eyeBaseSize,
        leftEyeCenter.dy
      );
      canvas.drawPath(leftEyePath, paint);

      final rightEyePath = Path();
      rightEyePath.moveTo(
        rightEyeCenter.dx - eyeBaseSize,
        rightEyeCenter.dy
      );
      rightEyePath.quadraticBezierTo(
        rightEyeCenter.dx,
        rightEyeCenter.dy - eyeBaseSize,
        rightEyeCenter.dx + eyeBaseSize,
        rightEyeCenter.dy
      );
      canvas.drawPath(rightEyePath, paint);
    } else if (intensity <= 2) {
      // 难过 - 弧形向下的眼睛
      final leftEyePath = Path();
      leftEyePath.moveTo(
        leftEyeCenter.dx - eyeBaseSize,
        leftEyeCenter.dy
      );
      leftEyePath.quadraticBezierTo(
        leftEyeCenter.dx,
        leftEyeCenter.dy + eyeBaseSize * 0.8,
        leftEyeCenter.dx + eyeBaseSize,
        leftEyeCenter.dy
      );
      canvas.drawPath(leftEyePath, paint);

      final rightEyePath = Path();
      rightEyePath.moveTo(
        rightEyeCenter.dx - eyeBaseSize,
        rightEyeCenter.dy
      );
      rightEyePath.quadraticBezierTo(
        rightEyeCenter.dx,
        rightEyeCenter.dy + eyeBaseSize * 0.8,
        rightEyeCenter.dx + eyeBaseSize,
        rightEyeCenter.dy
      );
      canvas.drawPath(rightEyePath, paint);
    } else {
      // 普通 - 点状眼睛
      canvas.drawCircle(leftEyeCenter, eyeBaseSize * 0.6, paint);
      canvas.drawCircle(rightEyeCenter, eyeBaseSize * 0.6, paint);
    }

    // 根据情绪强度画不同的嘴
    final mouthPath = Path();
    if (intensity >= 8) {
      // 开心 - 大弧度
      final mouthCenterY = center.dy + radius * 0.3;
      final mouthWidth = radius * 1.0;
      final mouthHeight = radius * 0.4;

      mouthPath.moveTo(
        center.dx - mouthWidth / 2,
        mouthCenterY
      );
      mouthPath.quadraticBezierTo(
        center.dx,
        mouthCenterY + mouthHeight,
        center.dx + mouthWidth / 2,
        mouthCenterY
      );
    } else if (intensity >= 6) {
      // 愉快 - 小弧度
      final mouthCenterY = center.dy + radius * 0.3;
      final mouthWidth = radius * 0.8;
      final mouthHeight = radius * 0.25;

      mouthPath.moveTo(
        center.dx - mouthWidth / 2,
        mouthCenterY
      );
      mouthPath.quadraticBezierTo(
        center.dx,
        mouthCenterY + mouthHeight,
        center.dx + mouthWidth / 2,
        mouthCenterY
      );
    } else if (intensity >= 4) {
      // 平静 - 直线
      final mouthY = center.dy + radius * 0.3;
      final mouthWidth = radius * 0.6;

      mouthPath.moveTo(center.dx - mouthWidth / 2, mouthY);
      mouthPath.lineTo(center.dx + mouthWidth / 2, mouthY);
    } else if (intensity >= 2) {
      // 低落 - 小弧度向下，更短更浅
      final mouthCenterY = center.dy + radius * 0.3;
      final mouthWidth = radius * 0.6; // 更短的宽度
      final mouthHeight = radius * 0.15; // 更浅的弧度

      mouthPath.moveTo(
        center.dx - mouthWidth / 2,
        mouthCenterY
      );
      mouthPath.quadraticBezierTo(
        center.dx,
        mouthCenterY - mouthHeight,
        center.dx + mouthWidth / 2,
        mouthCenterY
      );
    } else {
      // 难过 - 小嘴巴，深弧度向下
      final mouthCenterY = center.dy + radius * 0.3;
      final mouthWidth = radius * 0.5; // 更小的宽度
      final mouthHeight = radius * 0.3; // 保持较深的弧度

      mouthPath.moveTo(
        center.dx - mouthWidth / 2,
        mouthCenterY
      );
      mouthPath.quadraticBezierTo(
        center.dx,
        mouthCenterY - mouthHeight,
        center.dx + mouthWidth / 2,
        mouthCenterY
      );
    }

    canvas.drawPath(mouthPath, paint);
  }

  @override
  bool shouldRepaint(_HandDrawnEmojiPainter oldDelegate) {
    return oldDelegate.intensity != intensity || oldDelegate.color != color;
  }
} 